package com.ja.openchat.ui.chat.adapter.viewholder;

import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.call.CallManager;
import com.zy.openchat.core.model.call.LocalCallInfo;
import com.zy.openchat.core.model.message.UiMessage;
import com.zy.openchat.core.model.user.UserInfo;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.annotation.EnableContextMenu;
import com.ja.openchat.ui.chat.annotation.MessageContentType;

import butterknife.BindView;
import butterknife.OnClick;

@MessageContentType(TelegramManager.MessageType.MessageType_Text_AudioAVideo_Done)
@EnableContextMenu
public class CallMessageContentViewHolder extends NormalMessageContentViewHolder{

    @BindView(R.id.call_status)
    TextView contentTextView;
    public CallMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    @Override
    protected void onBind(UiMessage message) {
        LocalCallInfo callInfo = message.message.callInfo;
        if(callInfo==null){
            return;
        }
        int drawableResId;
        if(callInfo.isVideo){
            drawableResId = R.mipmap.chat_icon_shipin;
        }else{
            drawableResId = R.mipmap.chat_icon_guaduan;
        }
        contentTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(fragment.getContext(),drawableResId),null,null,null);
        contentTextView.setText(callInfo.displayDetailDesc());
    }

    @OnClick(R.id.call_status)
    public void onClick(View view) {
        LocalCallInfo callInfo = message.message.callInfo;
        if(callInfo==null){
            return;
        }
        UserInfo targetInfo = null;
        if(callInfo.from== UserInfo.getUserInfo().id){
            if(callInfo.to!=null&&callInfo.to.size()>0){
                targetInfo = UserInfo.getUserInfo(callInfo.to.get(0));
            }
        }else{
            targetInfo = UserInfo.getUserInfo(callInfo.from);
        }
        if(targetInfo!=null){
            CallManager.startC2CNewCall(fragment.getActivity(), targetInfo,callInfo.isVideo);
        }
    }
}
